package com.manor.main.activity;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.google.gson.Gson;
import com.lzy.okgo.model.Response;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.manor.common.CommonAppConfig;
import com.manor.common.Constants;
import com.manor.common.activity.AbsActivity;
import com.manor.common.http.HttpSignCallback;
import com.manor.common.http.JsonBeanX;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.StaIntentUtil;
import com.manor.common.utils.WordUtil;
import com.manor.main.R;
import com.manor.main.adapter.TXWithdrawalAdapter;
import com.manor.main.adapter.WithdrawalAdapter;
import com.manor.main.bean.TxListBean;
import com.manor.main.bean.WithdrawalBean;
import com.manor.main.http.MainHttpUtil;

/**
 * 收益二级列表
 */
@Route(path = RouteUtil.PC_Withdrawal)
public class WithdrawalRecordActivity extends AbsActivity {
    RecyclerView mRecyclerView ;
    WithdrawalAdapter mAdapter;
    TXWithdrawalAdapter txWithdrawalAdapter;
    private SmartRefreshLayout srlFollow;
    int page = 0;
    String type = "";
    private Gson mGson = new Gson();


    @Override
    protected int getLayoutId() {
        return R.layout.activity_withdrawal;
    }
    @Override
    protected void main() {

        if(getIntent().getIntExtra(Constants.MY_EARNINGS,0)== StaIntentUtil.NO_ENERGR){
            type = "2";
            setTitle("糖果");
        }
        if(getIntent().getIntExtra(Constants.MY_EARNINGS,0)== StaIntentUtil.EXTRACTABLE_ENERGY){
            type = "3";
            setTitle("嗨券");
        }
        if(getIntent().getIntExtra(Constants.MY_EARNINGS,0)== StaIntentUtil.FLASH_COUPONS){
            type = "5";
            setTitle("余额");
        }
        if(getIntent().getIntExtra(Constants.MY_EARNINGS,0)== StaIntentUtil.WITHDRAWAL_RECORD){
            type = "4";
            setTitle(WordUtil.getString(R.string.WITHDRAWAL_RECORD));
        }
        srlFollow = findViewById(R.id.srl_follow);
        mRecyclerView = findViewById(R.id.rc_listview);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));

        srlFollow.setEnableLoadMore(true);
        srlFollow.setEnableRefresh(true);

        srlFollow.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                setData(false);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                setData(true);
            }
        });
        setData(true);
    }

    private void setData(final boolean istype) {
        if(istype){
            page = 1;
        }else {
            page++;
        }
        if(type.equals("4")){
            MainHttpUtil.getWithdrawalTxList(CommonAppConfig.getInstance().getUid(), CommonAppConfig.getInstance().getToken(), page, new HttpSignCallback() {
                @Override
                public void onSuccess(int code, String msg, String info) {
                    if (code == 0 && info != null) {
                        TxListBean withdrawalBean = mGson.fromJson(info, TxListBean.class);
                        if (istype) {
                            txWithdrawalAdapter = new TXWithdrawalAdapter(R.layout.withdrawal_item, withdrawalBean.getList(), type);

                            txWithdrawalAdapter.setNewData(withdrawalBean.getList());
                            mRecyclerView.setAdapter(txWithdrawalAdapter);
                            srlFollow.finishRefresh();
                        } else {
                            txWithdrawalAdapter.addData(withdrawalBean.getList());
                            srlFollow.finishLoadMore();
                        }


                    }
                }

                @Override
                public void onError(Response<JsonBeanX> response) {
                    super.onError(response);
                    srlFollow.finishLoadMore();
                    srlFollow.finishRefresh();
                }
            });
        }else {
            MainHttpUtil.getWithdrawal(CommonAppConfig.getInstance().getUid(), page, type, new HttpSignCallback() {
                @Override
                public void onSuccess(int code, String msg, String info) {
                    if (code == 0 && info != null) {
                        WithdrawalBean withdrawalBean = mGson.fromJson(info, WithdrawalBean.class);
                        if (istype) {
                            mAdapter = new WithdrawalAdapter(R.layout.withdrawal_item, withdrawalBean.getData(), type);
                            mAdapter.setNewInstance(withdrawalBean.getData());
                            mRecyclerView.setAdapter(mAdapter);
                            srlFollow.finishRefresh();
                        } else {
                            mAdapter.addData(withdrawalBean.getData());
                            srlFollow.finishLoadMore();
                        }


                    }
                }

                @Override
                public void onError(Response<JsonBeanX> response) {
                    super.onError(response);
                    srlFollow.finishLoadMore();
                    srlFollow.finishRefresh();
                }
            });
        }
    }
}
